<template>
	<view class="sq_upopup">
		<u-popup :show="show" mode="center" @close="show=false">
			<view class="phone">
				<image src="https://i.ringzle.com/file/20240326/38841a80514e4ca98cdbb65d6d1673c2.png" class="bg" mode="widthFix"></image>
				<image src="https://fsy.shengsi.gov.cn/file/20240717/55b3617999594e08b2fd5bcbf2e3bacf.png" class="hi" mode="widthFix"></image>
				<button open-type="getPhoneNumber" @getphonenumber="decryptPhoneNumber">授权手机号登录</button>
			</view>
		</u-popup>
	</view>
</template>

<script>
	export default {
		options: {
			styleIsolation: 'shared'
		},
		data() {
			return {
				show:false
			}
		},
		methods: {
			//获取手机号进行登录
			decryptPhoneNumber(e){
				if(e.detail.code) this.wxLogin(e.detail.code);
			},
			//登录
			wxLogin(code){
				uni.showLoading({ title:'登录中' });
				let that = this;
				wx.login({
					success(res){
						that.$api.post('/auth/oauth/token',{
							code:res.code,
							phoneCode:code,
							grant_type:'wechat',
							captcha:'aaa'
						},false).then(res=>{
							that.show = false;
							if(res.data.code===0){
								uni.setStorageSync('authorization',res.data.token_type+' '+res.data.access_token);
								uni.setStorageSync('access_token',res.data.access_token);
								that.$api.get('/sys/user/info').then(res2=>{
									if(res2.data.code===0){
										uni.setStorageSync('userInfo',JSON.stringify(res2.data.data));
										that.$showToast('登录成功');
										uni.setStorageSync('isLogin',true);
										that.$emit('loginSuccess',res2);
									}else that.$showToast(res2.data.msg)
								})
							}else that.$showToast(res.data.msg)
						})
					}
				})
			}
		}
	}
</script>

<style lang="less">
	.sq_upopup {
		::v-deep .u-popup__content{
			width: 100%;
			height: 100%;
		}
		
		.phone{
			display: flex;
			flex-direction: column;
			align-items: center;
			
			.bg{
				width: 100%;
			}
			
			.hi{
				width: 352rpx;
				margin-top: 90rpx;
			}
			
			button{
				width: calc(100% - 120rpx);
				height: 88rpx;
				background: #007A69;
				border-radius: 44rpx;
				line-height: 88rpx;
				text-align: center;
				font-family: PingFangSC, PingFang SC;
				font-weight: 500;
				font-size: 32rpx;
				color: #FFFFFF;
				margin-top: 210rpx;
			}
		}
	}
</style>